import o from "./PropAniCtrl"
import i from "./SkillAniCtrl"
import r from "./MatchGameCenterCtrl"
import n from "./MatchGameViewCtrl"
import l from "./EventMgr"
import s from "./MatchGameEventType"
import c from "./EngineUtil"
import u from "./TimeUtils"
import p from "./frameworkManager"
import d, { MatchGameStatus } from "./MatchGameData"
import h from "./MatchGameConfig"
import f from "./FlyPropellerCtrl"
import g from "./ObstacleCtrl"
import y from "./MatchGameDataMgr"
import m from "./MatchGameEffectCtrl"
import _ from "./SquareStatusMgr"

const { ccclass, property } = cc._decorator;
@ccclass
class MatchGameLogicDataCtrl {

    public _map_row = h.MAX_ROW;
    public _map_col = h.MAX_COL;

    constructor() {
        this._map_row = h.MAX_ROW;
        this._map_col = h.MAX_COL;
    }

    static _instance;
    static _getInstance(): MatchGameLogicDataCtrl {
        this._instance || (this._instance = new MatchGameLogicDataCtrl());
        return this._instance;
    };

    clearUsedProp(e, t, a) {
        if (a) if (y.getPropItem(e, t) && _.getSquareReadyDestoryStatus(y.middleSquareGrid[e][t])) {
            y.destoryItem(y.middleSquareGrid[e][t]);
            g.onClearMatchs([{
                row: e,
                col: t
            }]);
        } else console.log("道具位置不能销毁？？？"); else if (y.getMiddleGridItem(e, t) && _.getSquareReadyDestoryStatus(y.middleSquareGrid[e][t])) {
            y.destoryItem(y.middleSquareGrid[e][t]);
            g.onClearMatchs([{
                row: e,
                col: t
            }]);
        } else console.log("道具位置2不能销毁？？？");
    };

    async clearRowCol(e, t, a, o, i, r, n?, l?) {
        void 0 === n && (n = false);
        void 0 === l && (l = false);
        cc.log("消除行列：", o, i, r, n, l);
        if (l) {
            this.clearUsedProp(o, i, true);
            r && this.clearUsedProp(r.i, r.j, false);
        }
        for (var s = .1, c = [], u = 0; u < this._map_row; u++) for (var d = 0; d < this._map_col; d++) {
            var h = Math.abs(u - o), f = Math.abs(d - i), g = 0;
            -1 != e && (g = f);
            -1 != t && (g = h);
            -1 != t && -1 != e && (g = h > f ? h : f);
            if (-1 != e && u >= e - a && u <= e + a || -1 != t && d >= t - a && d <= t + a) {
                var _ = y.setReadyDestoryByPos(u, d);
                if (!_) continue;
                var v = .08;
                -1 == t && (v = .04);
                // _.node.zIndex = 1;
                var b = (g *= v) + (a > 0 ? .5 : 0);
                s = Math.max(s, b);
                c.push({
                    item: _,
                    delyTime: g,
                    count: a > 0 ? .5 : 0
                });
            }
        }
        // var C = m.clearDelateItems(c, l ? {
        //     row: o,
        //     col: i
        // } : null);

        var C = m.clearDelateItems(c);

        m.clearRowColEffect(e, t, a, o, i, r, n, l);
        s = Math.max(s, C);
        p.log("火箭横竖消除时间---", s, c);
        return s;
    };
    public clearPropeller(e, t, a, i) {
        p.log("小飞机", e, t, a, i);
        var r;
        r = e == h.SquareType.PropItem;
        var n, l, s = [];
        n = a.i;
        l = a.j;
        if (!r || r && t !== h.PropType.LightingBall) {
            if (r) {
                n = i.i;
                l = i.j;
            } else {
                n = a.i;
                l = a.j;
                o.playOnePropUseBoomAni(h.PropType.Propeller, {
                    row: n,
                    col: l
                }, function () { });
            }
            var c = y.setReadyDestoryByPos(n, l + 1);
            c && s.push(c);
            var u = y.setReadyDestoryByPos(n, l - 1);
            u && s.push(u);
            var d = y.setReadyDestoryByPos(n + 1, l);
            d && s.push(d);
            var f = y.setReadyDestoryByPos(n - 1, l);
            f && s.push(f);
        } else y.matchDestoryItems([y.middleSquareGrid[n][l]], null);
        this.clearUsedProp(a.i, a.j, true);
        i && this.clearUsedProp(i.i, i.j, false);
        y.clearItems2(s, true);
        if (r) if (t == h.PropType.Propeller) for (var g = 1; g <= 3; g++) m.createPropeller(n, l, h.PropType.Propeller, g); else m.createPropeller(n, l, t, 0); else m.createPropeller(n, l, -1, 0);
        return .1;
    };
    public getOnePropllerDownPos(e) {
        var t, a = -1, o = 0, i = d.getAllTarget(true);
        if (e < 0 || e == h.PropType.Propeller) {
            var r = void 0;
            if (i && i.length) {
                a = 1;
                t = (r = d.duringGuide && 4 == d.gameLevel ? i[0] : i[c.getRandomNumber(0, i.length)])._row;
                o = r._col;
            } else if (r = this.getRandomSquareItem()) {
                a = 1;
                t = r._row;
                o = r._col;
            } else {
                a = 0;
                t = (l = this.getDisplayNormalPos())._row;
                o = l._col;
            }
        } else if (e == h.PropType.Tnt) {
            a = (n = this.getMaxAroundGridTargetPos(i)).count;
            t = (l = n.pos).row;
            o = l.col;
        } else if (e == h.PropType.RocketVertial) {
            a = (n = this.getMaxRocketRowColTargetPos(i, false)).count;
            t = (l = n.pos).row;
            o = l.col;
        } else if (e == h.PropType.RocketHorzontall) {
            var n, l;
            a = (n = this.getMaxRocketRowColTargetPos(i, true)).count;
            t = (l = n.pos).row;
            o = l.col;
        }
        p.log("获取小飞机降落地点", e, t, o, a);
        return {
            row: t,
            col: o,
            maxCount: a
        };
    };
    public getRandomSquareItem() {
        for (var e = r._instance.displayMapInfo, t = [], a = 0; a < this._map_row; a++) for (var o = 0; o < this._map_col; o++) if (e[a] && e[a][o] === h.DisplayMapType.Normal && y.middleSquareGrid[a] && y.middleSquareGrid[a][o]) {
            var i = y.middleSquareGrid[a][o];
            i && i.node && i.itemType && _.getSquareNormalStatus(i) && t.push(i);
        }
        return t.length ? t[c.getRandomNumber(0, t.length)] : null;
    };
    public getDisplayNormalPos() {
        for (var e = [], t = r._instance.displayMapInfo, a = 0; a < this._map_row; a++) for (var o = 0; o < this._map_col; o++) t[a][o] == h.DisplayMapType.Normal && e.push({
            row: a,
            col: o
        });
        return e[c.getRandomNumber(0, e.length)];
    };
    public getMaxRocketRowColTargetPos(e, t) {
        var a = this.getAllTargetDistributedMap(e), o = 0, i = null;
        if (t) for (var r = 0; r < h.MAX_COL; r++) for (var n = 0, l = 0; l < h.MAX_ROW; l++) {
            a[l] && a[l][r] && (n += a[l][r]);
            if (n > o) {
                o = n;
                i = {
                    row: l,
                    col: r
                };
            }
        } else for (l = 0; l < h.MAX_ROW; l++) {
            n = 0;
            var s = [];
            for (r = 0; r < h.MAX_COL; r++) if (a[l] && a[l][r]) {
                n += a[l][r];
                s.push(r);
            }
            if (n > o) {
                o = n;
                i = {
                    row: l,
                    col: s[c.getRandomNumber(0, s.length)]
                };
            }
        }
        if (!i) {
            var u = this.getRandomSquareItem();
            i = {
                row: u._row,
                col: u._col
            };
            o = 0;
        }
        return {
            count: o,
            pos: i
        };
    };
    public getAllTargetDistributedMap(e) {
        for (var t = [], a = 0; a < h.MAX_ROW; a++) {
            t[a] = [];
            for (var o = 0; o < h.MAX_COL; o++) t[a][o] = 0;
        }
        for (var i = 0; i < e.length; i++) {
            var r = e[i], n = r._row, l = r._col;
            t[n][l]++;
        }
        p.log("获取所有目标的地图分布", t);
        return t;
    };
    public getMaxAroundGridTargetPos(e) {
        for (var t = this.getAllTargetDistributedMap(e), a = 0, o = null, i = 2; i < h.MAX_ROW - 2; i++) for (var r = 2; r < h.MAX_COL - 2; r++) {
            for (var n = 0, l = -2; l <= 2; l++) for (var s = -2; s <= 2; s++) t[i + l] && t[i + l][r + s] && (n += t[i + l][r + s]);
            if (n > a) {
                a = n;
                o = {
                    row: i,
                    col: r
                };
            }
        }
        if (!o) {
            var c = this.getRandomSquareItem();
            o = {
                row: c._row,
                col: c._col
            };
            a = 0;
        }
        return {
            count: a,
            pos: o
        };
    };
    public getPropllerCanDown(e, t, a) {
        var o = d.getAllTarget(true);
        if (!o || !o.length) return true;
        var i = this.getAllTargetDistributedMap(o), r = 0;
        (a < 0 || a == h.PropType.Propeller) && i[e] && i[e][t] && (r = i[e][t]);
        a < 0 || a == h.PropType.Propeller ? i[e] && i[e][t] && (r = i[e][t]) : a == h.PropType.Tnt ? r = d.getGridCount(e, t, i) : a == h.PropType.RocketVertial ? r = d.getRocketRowColCount(e, t, false, i) : a == h.PropType.RocketHorzontall && (r = d.getRocketRowColCount(e, t, true, i));
        p.log("检测小飞机降落点的目标数量", r, e, t, a);
        return r > 0;
    };
    public onFlyItemRecover() {
        this.getFlyPropplerCount() <= 0 && this.onNoFlyItem();
    };
    public onNoFlyItem() {
        d.left_step <= 0 && d.gameStatus != MatchGameStatus.LostGame && y.setGlobalScheduler(function () {
            if (d.checkTargetIsFinish()) p.log("onNoFlyItem 目标完成"); else {
                p.log("onNoFlyItem 调用onGameFail");
                d.gameStatus = MatchGameStatus.LostGame;
                y.onGameFail();
            }
        }, 2);
    };
    public getFlyPropplerCount() {
        return f.getFlyPropplerCount();
    };
    public clearGrid(e, t, a?, o?) {
        var i = this;
        void 0 === t && (t = 4);
        void 0 === o && (o = true);
        p.log("消除方块", e, t, a);
        for (var r = [], n = Math.round(t % 2 == 0 ? t / 2 : (t - 1) / 2), c = 0; c < this._map_row; c++) for (var d = 0; d < this._map_col; d++) if ((c != e.row || d != e.col || !o) && (4 === t || c != a.row || d != a.col) && c >= e.row - n && c <= e.row + n && d >= e.col - n && d <= e.col + n) {
            var h = y.setReadyDestoryByPos(c, d);
            if (!h) continue;
            r.push(h);
            // h.node.zIndex = 1;
        }
        if (4 == t) {
            this.clearUsedProp(e.row, e.col, true);
            y.clearItems2(r, false);
            l.trigger(s.ADD_ALL_FILL, {
                callTime: u.getTimestamp(),
                tTime: 0
            });
            m.clearGridEffectSingle(e);
            return .2;
        }
        y.setGlobalScheduler(function () {
            i.clearUsedProp(e.row, e.col, true);
            a && i.clearUsedProp(a.row, a.col, false);
            y.clearItems2(r, false);
            l.trigger(s.ADD_ALL_FILL, {
                callTime: u.getTimestamp(),
                tTime: 0
            });
        }, 1);
        m.clearGridEffectMultiple(e);
        return 1;
    };
    public clearLightBall(e, t, a, o) {
        return m.clearLightBallEffect(e, t, a, o);
    };
    public clearLightCross(e, t) {
        var a = r._instance.displayMapInfo, o = [];
        a[e] && a[e][t + 1] && o.push({
            row: e,
            col: t + 1
        });
        a[e] && a[e][t - 1] && o.push({
            row: e,
            col: t - 1
        });
        a[e + 1] && a[e + 1][t] && o.push({
            row: e + 1,
            col: t
        });
        a[e - 1] && a[e - 1][t] && o.push({
            row: e - 1,
            col: t
        });
        g.onClearMatchs(o);
    };
    public updateLightBallCount(e) {
        y.lightingballAniCount += e;
        y.lightingballAniCount = Math.max(y.lightingballAniCount, 0);
        e <= 0 && n.instance.blockEliminate(false);
    };
    public useArrow(e) {
        var t, a, o = this;
        a = e.col;
        for (var l = h.MAX_ROW; l >= 0; l--) r._instance.displayMapInfo[l] && r._instance.displayMapInfo[l][e.col] && (t = l);
        i.playArrowAni({
            row: t,
            col: a
        }, function () { });
        n.instance.setPandaElimateStatus();
        p.log("使用弓箭", e);
        y.setGlobalScheduler(function () {
            o.clearRowCol(-1, e.col, 0, t, a, null, true);
        }, .1);
    };
    public useGun(e) {
        var t, a, o = this;
        t = e.row;
        for (var l = 0; l <= h.MAX_COL; l++) r._instance.displayMapInfo[e.row] && r._instance.displayMapInfo[e.row][l] && (a = l);
        n.instance.setPandaElimateStatus();
        p.log("技能释放位置：----", t, a);
        i.playGunAni({
            row: t,
            col: a - 1
        }, function () { });
        y.setGlobalScheduler(function () {
            o.clearRowCol(e.row, -1, 0, t, a, null, true, false);
        }, .8);
    };
    public useHammer(e) {
        n.instance.setPandaElimateStatus();
        y.setReadyDestoryByPos(e.row, e.col) && y.clearItems2([y.middleSquareGrid[e.row][e.col]], true);
    };
}


export default MatchGameLogicDataCtrl._getInstance();
